package com.czm.art_light.service.impl;

import com.czm.art_light.annotation.CacheWithLock;
import com.czm.art_light.constant.CommonConsts;
import com.czm.art_light.entity.ArtHelp;
import com.czm.art_light.mapper.ArtHelpMapper;
import com.czm.art_light.param.response.ArtHelpRespDto;
import com.czm.art_light.service.ArtHelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 帮组内容信息 服务类
 */
@Service
public class ArtHelpServiceImpl implements ArtHelpService {

    @Autowired
    private ArtHelpMapper artHelpMapper;

    /**
     * 返回全部的帮助内容
     */
    @CacheWithLock(
            cacheName = "allHelpContents",
            key = "",   //从参数对象提取属性
            expireTime = CommonConsts.HELP_CACHE_EXPIRATION_TIME,
            cacheNull = true,
            enabled = true
    )
    @Override
    public List<ArtHelpRespDto> getAllHelpContents() {
        List<ArtHelp> allHelpContents = artHelpMapper.getAllHelpContents();
        if (allHelpContents.isEmpty()) {
            return new ArrayList<>();  //返回空列表，如果没有数据就返回空列表，避免返回null
        }
        ArrayList<ArtHelpRespDto> helpRespDtoArrayList = new ArrayList<>();
        for (ArtHelp artHelp : allHelpContents) {
            helpRespDtoArrayList.add(ArtHelpRespDto.builder()
                    //帮组内容id
                    .id(artHelp.getId())
                    //帮助组内容回答
                    .answer(artHelp.getAnswer())
                    //帮助组内容问题
                    .question(artHelp.getQuestion())
                    .build());
        }
        return helpRespDtoArrayList;
    }
}
